package com.qf.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class MessageUtil {


    @Resource
    private RedisUtil redisUtil;

    public void sendMessage(String tel){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G8xQ5hG6DBg7NCpcsTD",
                "vIXi26MqDBKUGHerSJXRi2ENJghlJF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("SignName", "Giao言Giao语");
        request.putQueryParameter("TemplateCode", "SMS_192790131");
        double x = Math.random()*1000000;
        int y = (int) x;
        String s = Integer.toString(y);
        redisUtil.set(tel,s);
        String code = "{'code': '"+s+"'}";
        request.putQueryParameter("TemplateParam", code);
        //短信模板
        request.putQueryParameter("TemplateCode", "SMS_192790131");

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }
}
